package com.ruoyi.web.core.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Program: RuoYi-Vue-chy->ThreadPoolConfig
 * @Description: ThreadPoolExecutor 线程池
 * @Author: hongyu.chen
 * @Date: 2025-03-06 15:58
 * @Version： 1.0
 **/
@Configuration
public class ThreadPoolTestConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        int corePoolSize = Runtime.getRuntime().availableProcessors() * 2; // 核心线程数
        int maximumPoolSize = corePoolSize * 2; // 最大线程数
        long keepAliveTime = 60L; // 空闲线程存活时间
        TimeUnit unit = TimeUnit.SECONDS;
        int queueCapacity = 100; // 队列容量

        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                unit,
                new ArrayBlockingQueue<>(queueCapacity),
                // 使用默认线程工厂
                java.util.concurrent.Executors.defaultThreadFactory(),
                // 拒绝策略：由调用者线程执行
                new ThreadPoolExecutor.CallerRunsPolicy()
        );
    }
}
